const {ccclass, property} = cc._decorator;





/**聊天组件 */
@ccclass
export default class Chat extends cc.Component {

    /**右侧发起聊天的节点 */
    btnChat: cc.Node = null;

    /**聊天面板总容器 */
    chatRoot: cc.Node = null;

    /**预定定义好的聊天内容的容器 */
    tabQuick: cc.Node = null;

    /**emoji表情选择的容器 */
    tabEmoji: cc.Node = null;

    /**文字输入框 */
    iptChat: cc.EditBox = null;

    /**所有的快读聊天的内容及语音文件名 */
    quickChatInfo: cc.quickChatInfo = {};

    onLoad() {
        if(!cc.vv){
            return;
        }

        cc.vv.chat = this;
        this.btnChat = this.node.getChildByName('btn_chat');
        this.chatRoot = this.node.getChildByName('chat');
        this.tabQuick = this.chatRoot.getChildByName('quickchatlist');
        this.tabEmoji = this.chatRoot.getChildByName('emojis');
        this.iptChat = this.chatRoot.getChildByName('iptChat').getComponent(cc.EditBox);
        
        this.btnChat.active = cc.vv.replayMgr.isReplay() == false;
        this.chatRoot.active = false;

        this.quickChatInfo['item0'] = {
            index: 0,
            content: '快点啊，都等到我花儿都谢了！',
            sound: 'fix_msg_1.mp3'
        };

        this.quickChatInfo['item1'] = {
            index: 1,
            content: '怎么又断线了，网络怎么差啊！',
            sound: 'fix_msg_2.mp3'
        };

        this.quickChatInfo['item2'] = {
            index: 2,
            content: '不要走，决战到天亮！',
            sound: 'fix_msg_3.mp3'
        };

        this.quickChatInfo['item3'] = {
            index: 3,
            content: '你的牌打的也太好了！',
            sound: 'fix_msg_4.mp3'
        };

        this.quickChatInfo['item4'] = {
            index: 4,
            content: '你是妹妹还是哥哥啊？',
            sound: 'fix_msg_5.mp3'
        };

        this.quickChatInfo['item5'] = {
            index: 5,
            content: '和你合作太愉快了！',
            sound: 'fix_msg_6.mp3'
        };

        this.quickChatInfo['item6'] = {
            index: 6,
            content: '大家好很高兴见到各位！',
            sound: 'fix_msg_7.mp3'
        };

        this.quickChatInfo['item7'] = {
            index: 7,
            content: '各位，真不好意思，我得离开一会儿。',
            sound: 'fix_msg_8.mp3'
        };

        this.quickChatInfo['item8'] = {
            index: 8,
            content: '不要吵了，专心玩游戏吧！',
            sound: 'fix_msg_9.mp3'
        };
    }


    getQuickChatInfo(index){
        const key = 'item' + index;
        return this.quickChatInfo[key];
    }


    /**聊天组件背景点击事件(关闭聊天发送面板) */
    onBgClicked(){
        this.chatRoot.active = false;
    }

    /**右侧聊天图标的点击事件（显示聊天输入面板） */
    onBtnChatClicked(){
        this.chatRoot.active = true;
    }

    /**聊天发送面板的常用语跟表情按钮的点击事件（切换显示） */
    onTabClicked(event: cc.Event){
        if(event.target.name == 'tabQuick'){
            this.tabQuick.active = true;
            this.tabEmoji.active = false;
        }
        else if(event.target.name == 'tabEmoji'){
            this.tabQuick.active = false;
            this.tabEmoji.active = true;
        }
    }


    /**常用语每一句的点击事件 */
    onQuickChatItemClicked(event: cc.Event){
        this.chatRoot.active = false;
        const info = this.quickChatInfo[event.target.name];
        cc.vv.net.send(cc.vv.game_event_map.QUICK_CHAT, info.index);
    }

    /**每个emoji表情的点击事件 */
    onEmojiItemClicked(event: cc.Event){
        this.chatRoot.active = false;
        cc.vv.net.send(cc.vv.game_event_map.EMOJI, event.target.name);
    }

    /**聊天发送面板发送按钮的点击事件 */
    onBtnSendChatClicked(){
        this.chatRoot.active = false;
        if(this.iptChat.string == ''){
            return;
        }
        cc.vv.net.send(cc.vv.game_event_map.CHAT, this.iptChat.string);
        this.iptChat.string = '';
    }
}
